package com.ruoyi.hr.service.impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.mapper.PublicMapper;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.entryexitmanage.quitmanage.QuitManageMapper;
import com.ruoyi.hr.service.PublicService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.hr.util.utils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author yuanwei
 * @date 2020/4/18
 */
@Service
public class PublicServiceImpl implements PublicService {

    @Autowired
    private PublicMapper publicMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SkyMsgAlarmMapper skyMsgAlarmMapper;

    /**
     * 执行存储过程的公共方法（针对于各个模块添加员工的操作）
     *
     * @param allEid(编号)
     * @param allName(姓名)
     * @param procedure(存储过程名称)
     * @param type(业务类型参数)
     * @return
     */
    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult procedure(String allEid, String allName, String procedure, String type) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allEid.split(",");
            String[] name = allName.split(",");
            for (int i = 0; i < array.length; i++) {
                sql = "declare @RetVal varchar(50) exec " + procedure + " " + array[i] + "," + type + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = publicMapper.executeProcedure(sql);
                String msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    //根据PEID取出人名
                    msgAdd += name[i] + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    /**
     * 执行存储过程的公共方法（针对于确认，取消确认，执行流程这些操作）
     *
     * @param allId(编号)
     * @param allName(姓名)
     * @param procedure(存储过程名称)
     * @return
     */
    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult procedure(String allId, String allName, String procedure) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql = "";
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String[] name = allName.split(",");
            for (int i = 0; i < array.length; i++) {
                sql = "declare @RetVal varchar(50) exec " + procedure + " " + array[i] + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String msg = "";
                List<List<Map<String, Object>>> retVal = publicMapper.ExProcedure(sql);
                //如果大小为1直接取出返回值，先判断是否有key,再判断是否为数字执行
                if (retVal.size() == 1) {
                    if (((Map) retVal.get(0)).containsKey("")) {
                        String by = ((Map) retVal.get(0)).get("").toString();
                        if (utils.isInteger(by)) {
                            msg = skyMsgAlarmMapper.getMsgById(((Map) retVal.get(0)).get("").toString());
                        }
                    }
                } else if (retVal.size() > 1) {
                    for (List<Map<String, Object>> list : retVal) {
                        for (Map<String, Object> map : list) {
                            if (map.containsKey("")) {
                                String by = map.get("").toString();
                                if (utils.isInteger(by)) {
                                    String str = skyMsgAlarmMapper.getMsgById(map.get("").toString());
                                    if (str != null)
                                        msg += str;
                                }
                            }
                        }
                    }
                }
                if (msg != null && !msg.equals("") && !msg.equals("null")) {
                    msgAdd += name[i] + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            if (procedure.indexOf("Check") != -1) {
                return BaseResult.build(ResultEnum.SUCCESS, "数据确认成功!");
            } else if (procedure.indexOf("Cancel") != -1) {
                return BaseResult.build(ResultEnum.SUCCESS, "取消确认成功!");
            } else if (procedure.indexOf("Start") != -1) {
                return BaseResult.build(ResultEnum.SUCCESS, "执行操作成功!");
            }
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> addPerson(Map<String,Object> map) {
        return publicMapper.addPerson(utils.addAuthority(map));
    }

}
